package com.service.impl;

import com.entity.*;
import com.entity.DTO.PlaceOrder;
import com.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.List;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.utils.PageUtils;
import com.utils.Query;


import com.dao.YonghuDao;
import com.service.YonghuService;
import com.entity.vo.YonghuVO;
import com.entity.view.YonghuView;
import org.springframework.transaction.annotation.Transactional;

@Service("yonghuService")
@Slf4j
public class YonghuServiceImpl extends ServiceImpl<YonghuDao, YonghuEntity> implements YonghuService {
	
	
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<YonghuEntity> page = this.selectPage(
                new Query<YonghuEntity>(params).getPage(),
                new EntityWrapper<YonghuEntity>()
        );
        return new PageUtils(page);
    }
    
    @Override
	public PageUtils queryPage(Map<String, Object> params, Wrapper<YonghuEntity> wrapper) {
		log.info("yonghuserviceImpl.queryPage:{}",params);
		  Page<YonghuView> page =new Query<YonghuView>(params).getPage();
	        page.setRecords(baseMapper.selectListView(page,wrapper));
	    	PageUtils pageUtil = new PageUtils(page);
	    	return pageUtil;
 	}

	@Override
	@Transactional
	public void placeOrder(List<PlaceOrder> placeOrders, String userName, String phoneNum) {
		log.info("yonghuserviceImpl.placeOrder:{}{}",placeOrders,userName);
		try {
			//根据订单中的菜品id查询菜品信息
			List<Integer> listId = new ArrayList<>();
			for(PlaceOrder placeOrder : placeOrders) {
				listId.add(placeOrder.getMenuId());
			}
			List<CaipinxinxiEntity> caipinxinxiEntityList= baseMapper.getInfo(listId);

			//将菜品信息复制到订单中
			List<CaipindingdanEntity> caipindingdanEntityList = new ArrayList<>();
			for(int i = 0; i < caipinxinxiEntityList.size(); i++) {
				//获取菜品信息和数量
				CaipinxinxiEntity caipinxinxiEntity1 = caipinxinxiEntityList.get(i);
				Integer count = placeOrders.get(i).getCount();

				CaipindingdanEntity caipindingdanEntity = new CaipindingdanEntity();

				BeanUtils.copyProperties(caipinxinxiEntity1, caipindingdanEntity);
				caipindingdanEntity.setAddtime(new Date());
				caipindingdanEntity.setDingdanbianhao(CommonUtil.getRandomString(5));
				caipindingdanEntity.setXiadanshuliang(count);
				caipindingdanEntity.setYonghuming(userName);
				caipindingdanEntity.setXiadanshijian(new Date());
				caipindingdanEntity.setLianxidianhua(phoneNum);

				caipindingdanEntityList.add(caipindingdanEntity);
			}
			baseMapper.placeOrder(caipindingdanEntityList);
			log.info("添加订单成功");
		} catch (BeansException e) {
			log.info("placeOrder方法出现错误{}",e);
		}
	}

	@Override
	public String getDailyOrder(String userName) {
		List<CaipindingdanEntity> info = baseMapper.getDailyOrder(userName);
		if(info != null) {
			return "1";
		}
		return "2";
	}

	@Override
	public OrderFrom generateOrder(String userName) {
		List<CaipindingdanEntity> info = baseMapper.getDailyOrder(userName);
		List<OrderDetail> orderDetails = new ArrayList<>();
		for(CaipindingdanEntity caipindingdanEntity : info) {
			OrderFrom orderFrom = new OrderFrom();
			BeanUtils.copyProperties(caipindingdanEntity, orderDetails);
			//orderDetail
			//
			//orderFroms.add(orderFrom);
		}



		return null;
	}

	@Override
	public List<YonghuVO> selectListVO(Wrapper<YonghuEntity> wrapper) {
 		return baseMapper.selectListVO(wrapper);
	}
	
	@Override
	public YonghuVO selectVO(Wrapper<YonghuEntity> wrapper) {
 		return baseMapper.selectVO(wrapper);
	}
	
	@Override
	public List<YonghuView> selectListView(Wrapper<YonghuEntity> wrapper) {
		return baseMapper.selectListView(wrapper);
	}

	@Override
	public YonghuView selectView(Wrapper<YonghuEntity> wrapper) {
		return baseMapper.selectView(wrapper);
	}

}
